<?


class RPUBLIC extends ENGINE {
	function __construct() {
		
		// DECODE POST VARIABLES (if any)
		if( $_POST['set'] ) $this->set = json_decode( $_POST['set'] );


		// LOAD CLASSES
		$this->include_class(
			"mods/mod_SOCIAL.php", "mods/mod_MEDIA.php", "mods/mod_OBJECT.php", "mods/mod_TEXT.php"
		);

	}
	

/** 	MODULE FUNCTIONALITIES
* Still in doubt here.
* Should we include the mod_get function or keep it out? */


	////	LOAD MOD CLASS
	function mod_create ( $vars ) {
		require_once( ( file_exists ($custom = CLIENT_ROOT."/../custom.root/modules/{$vars->mod}/module.php"))?
			$custom : ROOT . "modules/{$vars->mod}/module.php" );
		$vars->modclass = $this->camelize( $vars->mod );
		return new $vars->modclass( $vars );
	}
	
	////	PARSE THE MOD(S)
	function mod_get($what, $var, $meta = false ) {
		global $DB, $Page;
		switch( $what ){
			
			case "all":
				
				// POPULATE WITH MAIN PAGE CONTENT
				if( $var=="main" && $Page->get('textid') ) {
					$MOD = $this->mod_create( json_decode('{"mod":"text.basic", "nameid":"'. $Page->get('nameid') .'", "textid":'. $Page->get('textid') .' }') );
					echo $MOD->output();	
				}
				
				// POPULATE WITH THE REST
				if( $column = $this->page->values['mods']->{$var} )

					foreach( $column as $node ) {
						$MOD = $this->mod_create( $DB->parse_json( $node ) );
						echo $MOD->output();
					}
				
				break;
		
			case "single":
				$MOD = $this->mod_create( $DB->parse_json( $var ) );
				echo $MOD->output();
				break;

			case "nth":
				if(count($this->page['mods'][$var]) > $meta) {
					$slice = array_splice( $this->page['mods'][$var], $meta, 1);
					$MOD = new MOD( $mod = key($slice), $slice[$mod]);
					$trg = explode('.',$mod);
					$modraw = include ( file_exists ($custom = CLIENT_ROOT."/../custom.root/mod.".$trg[0].".php"))?
						 $custom : ROOT."mods/".$trg[0].".php";
				}
				break;
		}
		if(strlen($modraw)) return $modraw;
	}

	function mod_construct_check() {
		if( $this->page->values['mods']->_construct_ ) {
			
			foreach( $this->page->values['mods']->_construct_ as $cname => $column ) {
				foreach( $column as $mod ) $this->page['mods']->{$cname}[] = $mod;
			
			}
			unset( $this->page->values['mods']->_construct_ );
		}
		//if( $_GET['id'] ) $this->objid = (int) $_GET['id'];
		//if( $this->page['mods']['_construct_'] ) $this->mod_get( "all", "_construct_");
	}
	
/** 	OUTPUT FUNCTIONALITIES
* A couple of generic page level output options
* Should be more generic in future. */

}

?>